//
//  PreferenceGiftBagTableViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/27.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import MJRefresh

// 特惠礼包

class PreferenceGiftBagTableViewController: BaseTableViewController {

    var headerImg:UIImageView!
    
    var dataArray = [SiteGoodsModel]()
    var page = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        
        self.title = "特惠礼包"
        initHeaderView()
        
        self.tableView.separatorStyle = .none
        self.tableView.rowHeight = 146
        
        self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: { [weak self] in
            self?.page = 1
            self?.getIndexRandom()
        })
        
        self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: { [weak self] in
            self?.page = (self?.page)! + 1
            self?.getIndexRandom()
        })
        PKhud.show()
        self.getIndexRandom()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "GoodsDetailViewController") as! GoodsDetailViewController
        ctrl.hidesBottomBarWhenPushed = true
        ctrl.goodsID = self.dataArray[indexPath.row].id
        ctrl.type = .spare
        self.navigationController?.pushViewController(ctrl, animated: true)
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row % 2 == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeftStyleTableViewCell", for: indexPath) as! LeftStyleTableViewCell
            cell.reload(with: self.dataArray[indexPath.row])
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "RightStyleTableViewCell", for: indexPath) as! RightStyleTableViewCell
            cell.reload(with: self.dataArray[indexPath.row])
            return cell
        }
    }
    
    // MARK:首页底部商品
    func getIndexRandom(){
        HttpRequest.requestData(.post, URLString: Api.indexBinding.rawValue, parameters: ["pageNum":self.page,"pageSize":10]) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                self.tableView.mj_header.endRefreshing()
                self.tableView.mj_footer.endRefreshing()
                if let array = [SiteGoodsModel].deserialize(from: result["data"] as? [Any]) as? [SiteGoodsModel] {
                    if self.page == 1 {
                        self.dataArray = array
                    }else{
                        self.dataArray = self.dataArray + array
                    }
                    if array.count < 10 {
                        self.tableView.mj_footer.endRefreshingWithNoMoreData()
                    }
                    self.getBanner()
                    self.tableView.reloadData()
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
    
    func getBanner(){
        HttpRequest.requestData(.post, URLString: Api.banner.rawValue, parameters: ["position":1]) { (result) in
            if result["msg"] as! String == "success"{
                if let dic = result["data"] as? postParameters {
                    self.headerImg.my_setImage(url: dic["img"] as! String, placeholder: Api.placeHolder.rawValue)
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }

}


extension PreferenceGiftBagTableViewController {
    
    func initHeaderView(){
        let header = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: (UIScreen.ScreenWidth - 24)/(352/180) + 30))
        header.backgroundColor = UIColor.nav_color
        let shadowLayer = CALayer()
        shadowLayer.frame = CGRect.init(x: 12, y: 15, width: UIScreen.ScreenWidth - 24, height: (UIScreen.ScreenWidth - 24)/(352/180))
        shadowLayer.backgroundColor = UIColor.nav_color.cgColor
        shadowLayer.shadowColor = UIColor.init(hexColor: "#465138").cgColor
        shadowLayer.shadowOffset = CGSize.init(width: 1.2, height: 1.7)
        shadowLayer.shadowOpacity = 0.4
        shadowLayer.cornerRadius = 15
        shadowLayer.shadowRadius = 3
        header.layer.addSublayer(shadowLayer)
        
        self.headerImg = UIImageView.init()
        self.headerImg.frame = shadowLayer.frame
        self.headerImg.setCornerRadio(radio: 15, isMask: true)
        header.addSubview(self.headerImg)
        
        self.tableView.tableHeaderView = header
    }
}





























